import { Nodes } from '@knno/dom';
import { Select as UISelect } from '../components/select/select';
import { RowType } from '../components/popList/popList';
import { JSXProps } from '@knno/dom/jsx';
import { AttrCheck, isArray, isBool, isFunc, isNumber, isObject, isStr, jsxBuild } from './utils';
import { ColumnOption } from '../components/list/list';
import { PopupHandler } from '../components/interface';
import { GridConfig } from '../components/grid/grid';

export { UISelect };

interface SelectProps extends JSXProps<UISelect> {
	config?: Partial<GridConfig>;
	clearable?: boolean;
	prefixIcon?: string;
	suffixIcon?: string;
	value?: unknown;
	placeholder?: string;
	disabled?: boolean;
	column?: ColumnOption;
	multiSelect?: boolean;
	filterable?: boolean;
	options?: RowType[];
	readonly?: boolean;
	disableBy?: string | number;
	invalid?: boolean;
	popupWidth?: string | number;
	onPopup?: PopupHandler<UISelect>;
}

const ATTRS: AttrCheck = {
	config: isObject,
	readonly: isBool,
	clearable: isBool,
	prefixIcon: isStr,
	suffixIcon: isStr,
	value: true,
	placeholder: isStr,
	maxLength: isNumber,
	column: isObject,
	multiSelect: isBool,
	filterable: isBool,
	options: isArray,
	disableBy: (v) => isStr(v) || isNumber(v),
	popupWidth: (v) => isStr(v) || isNumber(v),
	invalid: isBool,
	onPopup: isFunc,
};

export function Select(props: SelectProps, children: Nodes[]): UISelect {
	return jsxBuild(new UISelect(), props, children, ATTRS);
}
